/* beautify ignore:start */
import {Component, Input, OnInit, Output, EventEmitter, SimpleChange} from '@angular/core';
import {isPresent} from '../common';
/* beautify ignore:end */

const paginationConfig: any = {
    maxSize: 8,
    itemsPerPage: 15,
    boundaryLinks: false,// 显示首页尾页
    directionLinks: false,// 显示上一页下一页
    firstText: '首页',
    previousText: '上一页',
    nextText: '下一页',
    lastText: '尾页',
    makePage: false,  //跳转某页
    selectSize: true, //选择页面长度
    double: true //翻页
};

@Component({
    selector: 'table-pagination',
    styleUrls: ['table-pagination.component.css'],
    templateUrl: 'table-pagination.component.html'
})
export class TablePaginationComponent {
    @Input() public maxSize: number;    // 显示最大页数
    @Input() public boundaryLinks: boolean;  // 显示首页尾页
    @Input() public directionLinks: boolean; // 显示上一页下一页
    @Input() public makePage: boolean;      //跳转某页
    @Input() public selectSize: boolean;    //选择页面长度
    @Input() public double: boolean;        //翻页


    private pages: number; //总页面数
    private inputPage: any; //跳转页面输入框
    private disabled: boolean;  // 禁用


    @Input() public total: number; //总记录数
    @Input() public size: any; //每页行数
    @Input() public current: number; //当前页

    private pagination: Array<any>; //页面数据对象
    @Output() public pageChanged: EventEmitter<any> = new EventEmitter(); // 分页变化


    //初始化表格配置
    initSet() {
        this.maxSize = isPresent(this.maxSize) ? this.maxSize : paginationConfig.maxSize;
        this.boundaryLinks = isPresent(this.boundaryLinks) ? this.boundaryLinks : paginationConfig.boundaryLinks;
        this.directionLinks = isPresent(this.directionLinks) ? this.directionLinks : paginationConfig.directionLinks;
        this.makePage = isPresent(this.makePage) ? this.makePage : paginationConfig.makePage;
        this.selectSize = isPresent(this.selectSize) ? this.selectSize : paginationConfig.selectSize;
        this.double = isPresent(this.double) ? this.double : paginationConfig.double;
    }

    //初始化表格数据
    initData() {
        this.total = isPresent(this.total) ? this.total : 0;
        this.size = isPresent(this.size) ? this.size : paginationConfig.itemsPerPage;
        this.current = isPresent(this.current) ? this.current : 1;
    }

    // 计算最多页数
    private getPages(): number {
        let totalPages = this.total < 1 ? 1 : Math.ceil(this.total / this.size);
        return totalPages;
    }


    // 初始化
    ngOnInit() {
        this.initSet();
        this.initData();
    }


    //设置页面对象
    setPagination() {
        let pagination: Array<any> = [];
        this.pages = this.getPages();

        let startPage = this.current - Math.floor(this.maxSize / 2);
        let endPage = startPage + this.maxSize - 1;
        if (startPage <= 0) startPage = 1
        if (startPage == 1) endPage = this.maxSize;
        if (endPage > this.pages) endPage = this.pages;

        for (let index = startPage; index <= endPage; index++) {
            let page = this.setPage(index, index.toString(), index === this.current);
            pagination.push(page);
        }
        this.pagination = pagination;
    }


    // 设置页数对象,用于模板中
    private setPage(index: number, text: string, isActive: boolean): any {
        return {
            number: index,
            text: text,
            active: isActive
        };
    }

    private getText(key: string): string {
        return paginationConfig[key + 'Text'];
    }

    //改变行数
    private changeSize(event: any) {
        this.size = parseInt(event.target.value);
        this.current = 1;
        this.setPagination();
        this.pageChanged.emit({
            page: this.current,
            size: this.size
        });
    }


    // 属性变化
    ngOnChanges(changes: { [propertyName: string]: SimpleChange }) {
        for (let propName in changes) {
            // 总条数变更
            if ('total' === propName) this.total = changes[propName].currentValue;
            // 页面行数变更
            if ('size' === propName) this.size = changes[propName].currentValue;
            // 总条数变更
            if ('current' === propName) this.current = changes[propName].currentValue;
        }
        this.writeValue();
    }

    // 重新设置分页对象
    private writeValue() {
        this.setPagination();
    }


    // 是否有上一页
    private noPrevious(): boolean {
        return this.current === 1;
    }

    // 是否下一页
    private noNext(): boolean {
        return this.current === this.pages;
    }

    // 跳转某页
    private selectPage(page: number) {
        if (!this.isSafe(page)) return;
        this.current = page;
        this.writeValue();
        this.pageChanged.emit({
            page: this.current,
            size: parseInt(this.size)
        });

    }

    //跳转任意页
    private goPage() {
        let num = parseInt(this.inputPage);
        if (this.isSafe(num)) {
            this.selectPage(num);
        }
        this.inputPage = '';
    }

    //跳转页面是否合法
    isSafe(num: number) {
        return num > 0 && num <= this.pages
    }

    // 判断时候有没有数据
    private hasRow() {
        return this.total > 0;
    }

    //是否能上翻页
    private doublePre(): boolean {
        return (this.current - this.maxSize) >= 1;
    }

    //是否能下翻页
    private doubleNext(): boolean {
        return (this.current + this.maxSize) <= this.pages;
    }


}